<?php
namespace app\reduction\model;

use app\BaseModel;
use think\facade\Db;

class Active extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_reduction_active';


    protected $append=[
        //领取数量
        'get_num',
        //使用数量
        'use_num',
        //公司数量
        'company_num'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:16
     * @功能说明:领取数量
     */
    public function getGetNumAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'active_id' => $data['id']
            ];

            $user_record = new UserRecord();

            $num = $user_record->recordCount($dis);

            return $num;
        }
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:16
     * @功能说明:使用数量
     */
    public function getUseNumAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'active_id' => $data['id'],

                'status'     => 2,
            ];

            $user_record = new UserRecord();

            $num = $user_record->recordCount($dis);

            return $num;
        }
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:41
     * @功能说明:多少公司
     */
    public function getCompanyNumAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['uniacid'])&&isset($data['use_condition'])){

            if($data['use_condition']==1){
                $dis = [

                    'active_id' => $data['id'],

                    'status'    => 1
                ];
                $company = new Company();

                $num = $company->companyCount($dis);
            }else{

                $num = Db::name('longbing_card_company')->where(['uniacid'=>$data['uniacid']])->count();
            }
            return $num;
        }

    }



    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:03
     * @功能说明:添加活动
     */
    public function activeAdd($insert){

        $insert['create_time'] = time();

        $insert['update_time'] = time();

        $insert['status']      = 1;
        //赋值了就删掉
        if(isset($insert['company'])){

            $company = $insert['company'];

            unset($insert['company']);
        }
        Db::startTrans();
        $res = $this->insert($insert);
        //获取id
        $id  = $this->getLastInsID();
        //公司
        $company_model = new Company();
        //添加到每个公司
        if(!empty($company)){

            foreach ($company as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $insert['uniacid'],
                    //公司id
                    'company_id' => $v,
                    //活动id
                    'active_id'  => $id,

                    'status'     => 1
                ];

                $res = $company_model->companyAdd($insert);
            }
        }
        Db::commit();

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:06
     * @功能说明:活动详情
     */
    public function activeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:07
     * @功能说明:活动编辑
     */
    public function activeUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 11:27
     * @功能说明:后台的活动编辑
     */
    public function adminActiveUpdate($dis,$data){

        //赋值了就删掉
        if(isset($data['company'])){

            $company = $data['company'];

            unset($data['company']);
        }

        $res = $this->activeUpdate($dis,$data);

        $id  = $data['id'];

        //添加到每个公司
        if(!empty($company)){
            //公司
            $company_model = new Company();

            $c_dis[] = ['active_id','=',$id];

//            $c_dis[] = ['company_id','in',$company];

            $company_model->where($c_dis)->delete();

            foreach ($company as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $data['uniacid'],
                    //公司id
                    'company_id' => $v,
                    //活动id
                    'active_id'  => $id,

                    'status'     => 1
                ];

                $res = $company_model->companyAdd($insert);
            }
        }
        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:08
     * @功能说明:活动列表
     */
    public function activeList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 11:35
     * @功能说明:员工端的满减列表
     */
    public function staffActiveList($dis,$mapor){

        $data = $this->alias('a')
                ->join('longbing_card_reduction_company b','b.active_id = a.id','left')
                ->join('longbing_card_user_info c','c.company_id = b.company_id','left')
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field('a.*')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate(10)
                ->toArray();

        return $data;

    }








}